package com.neuedu.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.neuedu.bean.User;
import com.neuedu.dao.IUser;
import com.neuedu.util.Sqlutil;

public class UserImpl implements IUser {

	@Override
	@SuppressWarnings("unused")
	public List<User> selectAll() {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		List<User> listu=new ArrayList<>();
		Date date;
		try {
			conn=Sqlutil.getConn();
			String sql="select * from t_use";
			pstm=Sqlutil.getPrepareStatement(conn, sql);
			rs=Sqlutil.getResult(pstm);
			ResultSetIntoList(rs, listu);
		} catch (Exception e) {
		
			e.printStackTrace();
		}finally {
			Sqlutil.close(rs,pstm,conn);
		}
		
		
		
		return listu;
		
	}
	
	
	/**
	 * 对结果集的处理
	 * @throws SQLException 
	 */
	public static void ResultSetIntoList(ResultSet rs,List<User> listusers) throws SQLException {
		while(rs.next()) {
			User u=new User();
			u.setUid(rs.getInt("u_id"));
			u.setUname(rs.getString("u_name"));
			u.setUpwd(rs.getString("u_pwd"));
			u.setUaddress(rs.getString("u_address"));
			u.setUsex(rs.getString("u_sex"));
			listusers.add(u);
		}
	}


	@Override
	public int add(User user) {
		Connection conn=null;
		PreparedStatement pstm=null;
		int i=-1;
		try {
			conn=Sqlutil.getConn();
			String sql="insert into t_use (u_name,u_pwd,u_address) values(?,?,?)";
			pstm=Sqlutil.getPrepareStatement(conn, sql);
			Sqlutil.parameterIntoPreparedStatement(pstm, user.getUname(),user.getUpwd(),user.getUaddress());
			i=Sqlutil.getUpdate(pstm);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}


	@Override
	public int delete(int uid) {
	    Connection conn=null;
	    PreparedStatement pstm=null;
	    int i=-1;
	    
	    try {
			conn=Sqlutil.getConn();
			String sql="delete from t_use where u_id=?";
			pstm=Sqlutil.getPrepareStatement(conn, sql);
			Sqlutil.parameterIntoPreparedStatement(pstm, uid);
			i=Sqlutil.getUpdate(pstm);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			Sqlutil.close(pstm,conn);
		}
		return i;
	}


	@Override
	public User find(int uid) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		List<User> listu=new ArrayList<>();
		Date date;
		try {
			conn=Sqlutil.getConn();
			String sql="select * from t_use where u_id=?";
			pstm=Sqlutil.getPrepareStatement(conn, sql);
			Sqlutil.parameterIntoPreparedStatement(pstm, uid);
			rs=Sqlutil.getResult(pstm);
			ResultSetIntoList(rs, listu);
		} catch (Exception e) {
		
			e.printStackTrace();
		}finally {
			Sqlutil.close(rs,pstm,conn);
		}
		// List 是有序的集合，可以通过调用下标来时取值，使用get方法实现
		if(listu.size()==1) {
			return listu.get(0);
		}
		return null;
	}


	@Override
	public int update(User user) {
		Connection conn=null;
		PreparedStatement pstm=null;
		int i=-1;
		try {
			conn=Sqlutil.getConn();
			String sql="update t_use set u_name=?,u_pwd=?,u_address=? where u_id=?";
			pstm=Sqlutil.getPrepareStatement(conn, sql);
			Sqlutil.parameterIntoPreparedStatement(pstm, user.getUname(),user.getUpwd(),user.getUaddress(),user.getUid());
			i=Sqlutil.getUpdate(pstm);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			Sqlutil.close(pstm,conn);
		}
		return i;
	}


	@Override
	public User checkByNameandPassword(String name, String password) {
		
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		List<User> listu=new ArrayList<>();
		Date date;
		try {
			conn=Sqlutil.getConn();
			String sql="select * from t_use where u_name=? and u_pwd=?";
			pstm=Sqlutil.getPrepareStatement(conn, sql);
			Sqlutil.parameterIntoPreparedStatement(pstm, name,password);
			rs=Sqlutil.getResult(pstm);
			ResultSetIntoList(rs, listu);
		} catch (Exception e) {
		
			e.printStackTrace();
		}finally {
			Sqlutil.close(rs,pstm,conn);
		}
		// List 是有序的集合，可以通过调用下标来时取值，使用get方法实现
		if(listu.size()==1) {
			return listu.get(0);
		}
		return null;
	}


	@Override
	public List<User> selectUserByCondition(String condition) {
		// TODO Auto-generated method stub
		return null;
	}

}
